package com.gaogzhen.algorithm.leetCode.daily;

/**
 * 题目：使字符串平衡的最少删除次数
 * 地址：https://leetcode.cn/problems/minimum-deletions-to-make-string-balanced/
 * @author: Administrator
 * @createTime: 2023/03/06 21:47
 */
public class D20230306 {
    public int minimumDeletions(String s) {
        int len = s.length();
        int h = 0,t = len - 1;
        while (h < len && s.charAt(h) == 'a' ) {
            h++;
        }
        if (h == len) {
            return 0;
        }
        while (t >= 0 && s.charAt(t) == 'b') {
            t--;
        }

        if (t == -1 || h - t == 1) {
            return 0;
        }
        int ac = 0;
        for (int i = h; i <= t ; i++) {
            if (s.charAt(i) == 'a') {
                ac++;
            }
        }
        int bc = t - h + 1 - ac;
        System.out.println(ac + "==" + bc);
        if (ac < bc) {
            t = len - 1;
            while (s.charAt(t--) == 'a') {
                ac--;
            }
            return ac;
        } else {
            h = 0;
            while (s.charAt(h++) == 'b') {
                bc--;
            }
            return bc;
        }

    }
}
